<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		header("Content-type: text/html; charset=utf-8");
		$this->lang->load('ajax', $this->session->userdata['user_lang']);
	}
	
	public function get_site_type()
	{
		$this->load->model('Site_type_model', 'site_type');
		$type_list = $this->site_type->get();
	       	
		$comboSite = '<select name="idtype" id="idtype" class="inputdiv">';
		$comboSite .= '<option value="">'.$this->lang->line('ajax_selectsitetype').'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$comboSite .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$comboSite .= '</select>';
		
		exitHTML($comboSite);
	}
	
	public function get_course_level($object_name = 'CandidateCourse')
	{
		$this->load->model('Course_level_model', 'course_level');
		$type_list = $this->course_level->get();
	       	
		$comboLevel = '<select name="idlevel" id="idlevel" class="inputdiv" onchange="'.$object_name.'.getCourseType();">';
        $comboLevel .= '<option value="">'.$this->lang->line('ajax_selectcourselevel').'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$comboLevel .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$comboLevel .= '</select>';
		
		exitHTML($comboLevel);
	}
	
	public function get_course_type()
	{
		$data = $_POST;
		$this->load->model('Course_type_model', 'course_type');
		$type_list = $this->course_type->get(array('idlevel' => $data['idlevel']));
	       	
		$combo = '<select name="idcoursetype" id="idcoursetype" class="inputdiv">';
        $combo .= '<option value="">'.$this->lang->line('ajax_selectcoursetype').'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_course_status()
	{
		$this->load->model('Course_status_model', 'status');
		$type_list = $this->status->get();
	       	
		$combo = '<select name="idstatus" id="idstatus" class="inputdiv">';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_country($select_type = '', $default_text = '')
	{
		$this->load->model('Country_model', 'country');
		$type_list = $this->country->get();
	       	
		$combo = '<select name="idcountry" id="idcountry" class="inputdiv"'.($select_type == 'multiple' ? ' multiple="multiple"' : '').' onchange="getState(null, '.(isset($_POST['idcountry']) ? ("'".$_POST['idcountry']."'") : 'null').', \''.($default_text == 'any' ? '/multiple/any' : '').'\');">';
        
		$first_option_trans = 'ajax_selectcountry';
		
		if ($default_text == 'any')
		{
			$first_option_trans = 'ajax_anycountry';
		}
		
		$combo .= '<option value="">'.$this->lang->line($first_option_trans).'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'"'.($row->id == 18 && $default_text != 'any' ? 'selected="selected"' : '').'>'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_state($select_type = '', $default_text = '')
	{
		$data = $_POST;
		$this->load->model('State_model', 'state');
		$type_list = $this->state->get($data);
	       	
		$combo = '<select name="idstate" id="idstate" class="inputdiv"'.($select_type == 'multiple' ? ' multiple="multiple"' : '').'>';
        
		if ($default_text == 'any')
		{
			$combo .= '<option value="">'.$this->lang->line('ajax_anystate').'</option>';
		}
		
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'"'.($row->id == 25 && $default_text != 'any' ? 'selected="selected"' : '').'>'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_job_hierarchy($select_type = '', $default_text = '')
	{
		$this->load->model('Job_hierarchy_model', 'job_hierarchy');
		$type_list = $this->job_hierarchy->get();
		
		$combo = '<select name="idhierarchy" id="idhierarchy" class="inputdiv"'.($select_type == 'multiple' ? ' multiple="multiple"' : '').'>';
		
		$first_option_trans = 'ajax_selectjobhierarchy';
		
		if ($default_text == 'any')
		{
			$first_option_trans = 'ajax_anyjobhierarchy';
		}
		
		$combo .= '<option value="">'.$this->lang->line($first_option_trans).'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_job_category($javascript_object = '')
	{
		$this->load->model('Job_category_model', 'job_category');
		$type_list = $this->job_category->get();
		
		if (!$javascript_object)
		{
			$javascript_object = 'CandidateExperience';
		}
	       	
		$combo = '<select name="idcategory" id="idcategory" class="inputdiv" onchange="'.$javascript_object.'.getJobSpecialty();">';
		$combo .= '<option value="">'.$this->lang->line('ajax_selectjobcategory').'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_job_specialty($select_type = '', $default_text = '')
	{
		$data = $_POST;
		
		$this->load->model('Job_specialty_model', 'job_specialty');
		$type_list = $this->job_specialty->get($data);
	       	
		$combo = '<select name="idspecialty" id="idspecialty" class="inputdiv"'.($select_type == 'multiple' ? ' multiple="multiple"' : '').'>';
		
		$first_option_trans = 'ajax_selectjobspecialty';
		
		if ($default_text == 'any')
		{
			$first_option_trans = 'ajax_anyjobspecialty';
		}
		
		$combo .= '<option value="">'.$this->lang->line($first_option_trans).'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_genre()
	{
		$data = $_POST;
		
		$this->load->model('Genre_model', 'genre');
		$type_list = $this->genre->get($data);
	       	
		$combo = '<select name="idgenre" id="idgenre" class="inputdiv">';
		$combo .= '<option value="">'.$this->lang->line('ajax_selectgenre').'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_marital_status()
	{
		$data = $_POST;
		
		$this->load->model('Marital_status_model', 'marital_status');
		$type_list = $this->marital_status->get($data);
	       	
		$combo = '<select name="idmaritalstatus" id="idmaritalstatus" class="inputdiv">';
		$combo .= '<option value="">'.$this->lang->line('ajax_selectmaritalstatus').'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_drive_license($default_text = '')
	{
		$data = $_POST;
		
		$this->load->model('Drive_license_model', 'drive_license');
		$type_list = $this->drive_license->get($data);
		
		$default_text = $default_text == 'optional' ? 'ajax_optionaldrivelicense' : 'ajax_selectdrivelicense';
	       	
		$combo = '<select name="iddrivelicense" id="iddrivelicense" class="inputdiv">';
		$combo .= '<option value="">'.$this->lang->line($default_text).'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_zipcode_address()
	{
		$data = $_POST;
		$matches = null;
		
		if ('cep.republicavirtual.com.br' != gethostbyname('cep.republicavirtual.com.br')) {
			// Web Service Republica Virtual.
			$ws_url = file_get_contents("http://cep.republicavirtual.com.br/web_cep.php?cep=".$data['zipcode']."&formato=xml");
			$ws_pattern = "|<uf>(.*?)</uf>(.*?)<cidade>(.*?)</cidade>(.*?)<bairro>(.*?)</bairro>(.*?)<tipo_logradouro>(.*?)</tipo_logradouro>(.*?)<logradouro>(.*?)</logradouro>|s";
			// Retorna dados do web service utilizado.
			preg_match_all($ws_pattern, utf8_encode($ws_url), $matches);
		}
		
		if (!$matches)
		{
			formError($this->lang->line('global_systemfail'), true);
		}
		
		$neighborhood = $matches[5][0];
		$state_short_name = $matches[1][0];
		$idstate = '';
		$city = $matches[3][0];
		$address = $matches[7][0] . ' ' . $matches[9][0];
		
		if ($state_short_name != '')
		{
			$this->load->model('State_model', 'state');
			$idstate = $this->state->get(array('shortname' => $state_short_name))->row()->id;
		}
		
		$script = '';
		$script .= '$("#neighborhood").val("'.str_replace('"', '\"', $neighborhood).'");';
		$script .= '$("#city").val("'.str_replace('"', '\"', $city).'");';
		$script .= '$("#address").val("'.str_replace('"', '\"', $address).'");';
		$script .= '$("#idstate").val("'.$idstate.'");';
		$script .= '$("#number").focus();';
		
		exitJs($script);
	}
	
	public function get_language_type()
	{
		$data = $_POST;
		$this->load->model('Language_model', 'language');
		$type_list = $this->language->get();
	       	
		$combo = '<select name="idlanguagetype" id="idlanguagetype" class="inputdiv">';
        $combo .= '<option value="">'.$this->lang->line('ajax_selectlanguage').'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_language_level()
	{
		$data = $_POST;
		$this->load->model('Language_level_model', 'language_level');
		$type_list = $this->language_level->get();
	       	
		$combo = '<select name="idlanguagelevel" id="idlanguagelevel" class="inputdiv">';
        $combo .= '<option value="">'.$this->lang->line('ajax_selectlanguagelevel').'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_company_category()
	{
		$data = $_POST;
		
		$this->load->model('Company_category_model', 'category');
		$type_list = $this->category->get($data);
	       	
		$combo = '<select name="idcategory" id="idcategory" class="inputdiv">';
		$combo .= '<option value="">'.$this->lang->line('ajax_selectcompanycategory').'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'">'.ucfirst($row->name).'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function get_company_size()
	{
		$data = $_POST;
		
		$this->load->model('Company_size_model', 'size');
		$type_list = $this->size->get($data);
	       	
		$combo = '<select name="idsize" id="idsize" class="inputdiv">';
		$combo .= '<option value="">'.$this->lang->line('ajax_selectcompanysize').'</option>';
        
        foreach ($type_list->result() as $row)
		{
			$combo .= '<option value="'.$row->id.'">'.$row->name.'</option>';
		}
		
		$combo .= '</select>';
		
		exitHTML($combo);
	}
	
	public function check_subscription_status()
	{
		$data = $_POST;
		$this->load->model('Subscription_model', 'subscription');
		
		switch ($data['idstatus'])
		{
			case 3:
				$this->subscription->check_assessment($data['idsubscription']);
				break;
				
			case 4:
				$this->subscription->check_finalist($data['idsubscription']);
				break;

			case 5:
				$this->subscription->check_discarded($data['idsubscription']);
				break;
				
			case 6:
				$this->subscription->check_engaged($data['idsubscription']);
				break;
		}
		
		exitHTML();
	}
}

?>